﻿using System;
using System.Collections.Generic;
using System.Text;
using NORM.Entity;

namespace NORM.SQLObject
{
    public class OQLUpdate : OQLComponent
    {
        private OQL currentOql = null;

        private OQLUpdate()
        {
            this.Key = "OQLUpdate";
            this.Guid = "3c4636d9-9043-4f04-b4a2-1949cdc4f7ee";
        }

        public OQLUpdate(OQL oql)
            : this()
        {
            currentOql = oql;
        }

        public bool Set(object Field)
        {
            if (currentOql != null)
            {
                object ovalue = Field;
                string fieldstring = Field + "";
                if (currentOql.fieldStack.Count > 0)
                {
                    TableField tf = currentOql.fieldStack.Pop();
                    fieldstring = tf.Name;
                }

                TableField ntf = new TableField();
                ntf.Entity = currentOql.Modelobject;
                ntf.Name = fieldstring;
                ntf.Value = ovalue;

                List<TableField> list = NORM.Common.ArrayPlus.ToList<TableField>(currentOql.UpdateFields);
                list.Add(ntf);

                currentOql.Update(list.ToArray());

                return true;

            }

            return false;

        }
    }
}
